<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="电话号码">
            <a-input v-decorator="['search']" placeholder="请输入电话号码" />
          </a-form-item>
          <a-form-item label="添加时间">
            <a-range-picker format="YYYY-MM-DD" v-decorator="['betweenTime']" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>
      <a-row>
        <a-button v-action:add type="primary" icon="plus" @click="addHospital">新增</a-button>
      </a-row>
    </div>
    <s-table
      ref="table"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="item">
        <a title="修改" v-action:recharge @click="edit(item)">修改</a>
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/project/health'
import { STable } from '@/components'
import { mpweixin, h5, gweixin } from '@/core/icons'

// 注册来源名称
const PlatformName = {
  'MP-WEIXIN': '微信小程序',
  'H5': 'H5',
  'APP': 'APP',
  'G-WEIXIN': '微信公众号'
}

// 注册来源图标
const PlatformIcons = {
  'MP-WEIXIN': mpweixin,
  'H5': h5,
  'G-WEIXIN': gweixin
}

export default {
  name: 'Index',
  components: {
    STable
  },
  data () {
    return {
      // 注册来源名称
      PlatformName,
      // 注册来源图标
      PlatformIcons,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: 'ID',
          dataIndex: 'id'
        },
        {
          title: '姓名',
          dataIndex: 'name'
        },
        {
          title: '电话',
          dataIndex: 'mobile'
        },
        {
          title: '身份证号',
          dataIndex: 'idCard'
        },
        {
          title: '生日',
          dataIndex: 'birthday'
        },
        {
          title: 'BMI',
          dataIndex: 'bmi'
        },
        {
          title: '地区',
          dataIndex: 'address'
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          // dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.getList({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      },
      // 会员等级列表
      gradeList: []
    }
  },
  created () {

  },
  methods: {

    // 添加
    addHospital () {
      this.$router.push('/project/manage/health_add')
    },

    // 修改
    edit (item) {
      this.$router.push({ path: '/project/manage/health_edit', query: { id: item.id } })
    },
    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ form: { id: item.id } })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
.platform {
  .icon {
    font-size: 17px;
  }

  .MP-WEIXIN {
    color: #04be02;
  }
  .H5 {
    color: #e44c27;
  }
}
</style>
